/**
 * 
 */
package pt.inescid.components.messages;

import java.io.IOException;

import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.Id;
import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.NodeHandle;
import rice.p2p.commonapi.NodeHandleSet;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import rice.p2p.commonapi.rawserialization.RawMessage;
import rice.pastry.NodeSet;

/**
 * @author Sergio Esteves (sesteves at gsd.inesc-id.pt)
 *
 */
public class ACKMsg implements RawMessage {

	public static final short TYPE = 4;
	
	public final static short ACK = 0;
	public final static short NACK = 1;
	
	private NodeHandleSet source;
	
	private short msgType = 0; 	
	
	private static final long serialVersionUID = 7526472295622776147L;
	
	public ACKMsg(NodeHandle source) {
		this.source = new NodeSet();
		this.source.putHandle(source);
	}

	public ACKMsg(NodeHandle source, short msgType) {
		this(source);
		this.msgType = msgType;
	}
	
	public short getType() {		
		return TYPE;
	}

	public int getPriority() {
		return Message.LOW_PRIORITY;
	}
	
	public Id getSource() {
		return source.getHandle(0).getId();
	}
	
	public String toString() {
		return "source: " + getSource() + ", msgType: " + msgType;
	}
	
	public ACKMsg(InputBuffer buf, Endpoint endpoint) throws IOException {
		source = endpoint.readNodeHandleSet(buf, buf.readShort());
		msgType = buf.readShort();
	}

	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(source.getType());
		source.serialize(buf);
		buf.writeShort(msgType);
	}
}
